package com.siyi.fpvdemo;


public class Mk15Key {
    public int mChannel = 0;
    public int mDefault = 1500;
    public int mValue = 0;
    public boolean isPressed = false;
    public static int mRunSpeed = 2000;

    public Mk15Key(int _channel, int _default) {
        mChannel = _channel;
        mDefault = _default;
    }

    public static void setSpeed(int speed) {
        mRunSpeed = speed;
        System.out.println("Set run speed: " + speed);
    }

    public enum Status { DownState, StopState, UpState };
    public enum KeyId {
                Key_J4(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        byte[] cmd = new byte[4];
                        cmd[0] = 0x02;
                        if (Status.DownState == _state) {
                            cmd[1] = 0x03;
                        } else if (Status.StopState == _state) {
                            cmd[1] = 0x02;
                        } else if (Status.UpState == _state) {
                            cmd[1] = 0x04;
                        }
                        cmd[2] = (byte)((mRunSpeed >> 8) & 0xff);
                        cmd[3] = (byte)(mRunSpeed & 0xff);
                        return cmd;
                    }
                })  ,
                Key_J3(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                }),
                Key_J2(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                }),
                Key_J1(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        // 步进电机1控制---左臂
                        byte[] cmd = new byte[7];
                        cmd[0] = 0x03;
                        cmd[1] = 0x03;  // device_id
                        if (Status.DownState == _state) {
                            cmd[2] = 0x01;
                        } else if (Status.StopState == _state) {
                            cmd[2] = 0x00;
                        } else if (Status.UpState == _state) {
                            cmd[2] = 0x02;
                        }
                        int speed = 450000;
                        for (int i = 0; i < 4; i++) {
                            cmd[3 + i] = (byte) ((speed >> ((3 - i) * 8)) & 0xff);
                        }
                        return cmd;
                    }
                }),
                Key_SA(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        // 步进电机1控制---横轴
                        byte[] cmd = new byte[7];
                        cmd[0] = 0x03;
                        cmd[1] = 0x01;  // device_id
                        if (Status.DownState == _state) {
                            cmd[2] = 0x02;
                        } else if (Status.StopState == _state) {
                            cmd[2] = 0x00;
                        } else if (Status.UpState == _state) {
                            cmd[2] = 0x01;
                        }
                        int speed = 450000;
                        for (int i = 0; i < 4; i++) {
                            cmd[3 + i] = (byte) ((speed >> ((3 - i) * 8)) & 0xff);
                        }
                        return cmd;
                    }
                }),
                Key_SB(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        // 步进电机2控制---右臂
                        byte[] cmd = new byte[7];
                        cmd[0] = 0x03;
                        cmd[1] = 0x02;  // device_id
                        if (Status.DownState == _state) {
                            cmd[2] = 0x02;
                        } else if (Status.StopState == _state) {
                            cmd[2] = 0x00;
                        } else if (Status.UpState == _state) {
                            cmd[2] = 0x01;
                        }
                        int speed = 450000;
                        for (int i = 0; i < 4; i++) {
                            cmd[3 + i] = (byte) ((speed >> ((3 - i) * 8)) & 0xff);
                        }
                        return cmd;
                    }
                }),
                Key_SC(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        byte[] cmd = new byte[2];
                        cmd[0] = 0x05;
                        if (Status.DownState == _state) {
                            cmd[1] = 0x01;
                        }
                        return cmd;
                    }
                }),
                Key_LD(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        // 步进电机5控制---左边方向
                        byte[] cmd = new byte[7];
                        cmd[0] = 0x03;
                        cmd[1] = 0x05;  // device_id
                        if (Status.DownState == _state) {
                            cmd[2] = 0x01;
                        } else if (Status.StopState == _state) {
                            cmd[2] = 0x00;
                        } else if (Status.UpState == _state) {
                            cmd[2] = 0x02;
                        }
                        int speed = 450000;
                        for (int i = 0; i < 4; i++) {
                            cmd[3 + i] = (byte) ((speed >> ((3 - i) * 8)) & 0xff);
                        }
                        return cmd;
                    }
                }),
                Key_RD(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        // 步进电机4控制---左边方向
                        byte[] cmd = new byte[7];
                        cmd[0] = 0x03;
                        cmd[1] = 0x04;  // device_id
                        if (Status.DownState == _state) {
                            cmd[2] = 0x01;
                        } else if (Status.StopState == _state) {
                            cmd[2] = 0x00;
                        } else if (Status.UpState == _state) {
                            cmd[2] = 0x02;
                        }
                        int speed = 450000;
                        for (int i = 0; i < 4; i++) {
                            cmd[3 + i] = (byte) ((speed >> ((3 - i) * 8)) & 0xff);
                        }
                        return cmd;
                    }
                }),
                Key_A(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                }),
                Key_B(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                }),
                Key_C(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                }),
                Key_D(new MakeRobotPackage() {
                    @Override
                    public byte[] makePackage(Status _state) {
                        return new byte[0];
                    }
                });

            private MakeRobotPackage makeRobotPackage;
            private KeyId(MakeRobotPackage makeRobotPackage) {
                this.makeRobotPackage = makeRobotPackage;
            }

            public MakeRobotPackage getMakeRobotPackage(){
                return this.makeRobotPackage;
            }

            public static  KeyId getKeyIdByIndex(int index){
                for(KeyId key:KeyId.values()){
                    if(index == key.ordinal()){
                        return key;
                    }
                }
                throw new RuntimeException("KeyId enum index out range");
            }
    }

    public void setValue(int _value, Mk15Callback mk15Callback) {
        if (_value != mDefault) {
            if (isPressed || mValue == _value) {return;}

            if (_value == 1050) {
                isPressed = true;
                mk15Callback.OnClicked(mChannel, Status.DownState);
            } else if (_value == 1950) {
                isPressed = true;
                mk15Callback.OnClicked(mChannel, Status.UpState);
            }
        } else if (_value == mDefault && isPressed){
            isPressed = false;
            mk15Callback.OnClicked(mChannel, Status.StopState);
        }

        mValue = _value;
    }
}
